package dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;



public class JDBCUtil {
	private static Connection connection;
	private static PreparedStatement preparedStatement=null;
	private static String url="jdbc:mysql://localhost:3308/meeting?useSSL=false&serverTimezone=UTC";
	private static String username="root";
	private static String password="Admin@123";
	public static Connection getConnection() {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			connection=DriverManager.getConnection(url,username,password);
			return connection;
		} catch (ClassNotFoundException | SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}

	}

	public static void closeAll() {
		try {
			preparedStatement.close();
			connection.close();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

	}
	public static int execStatement(String sql,List<Object> params) {
		try {
			preparedStatement=getConnection().prepareStatement(sql);
			//遍历参数集合，逐一赋值给preparedStatement对象的sql语句里的‘？’
			for(int i=0;i<params.size();i++)
				preparedStatement.setObject(i+1, params.get(i));
			//执行sql语句
			return preparedStatement.executeUpdate();
			
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return -1;
		}
				
	}
	public static ResultSet execQuery(String sql,List<Object> params) {
		try {
			preparedStatement=getConnection().prepareStatement(sql);
			//遍历参数集合，逐一赋值给preparedStatement对象的sql语句里的‘？’
			for(int i=0;i<params.size();i++)
				preparedStatement.setObject(i+1, params.get(i));
			//执行sql语句
			return preparedStatement.executeQuery();
			
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}
				
	}
}
